package controller;

import dao.BookDao;
import dao.BookRecordDao;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.Book;
import model.BookRecord;
import model.MemberUser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

@WebServlet(name = "BookServlet", value = "/Book")
public class BookServlet extends HttpServlet {
    public static final Logger logger = LogManager.getLogger(BookServlet.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method") == null ? "" : request.getParameter("method");
        RequestDispatcher requestDispatcher;
        ServletContext servletContext = this.getServletContext();
        BookDao bookDao = new BookDao();
        BookRecordDao bookRecordDao = new BookRecordDao();
        // 搜索图书
        if ("search".equals(method)) {
            Book book = (Book) request.getAttribute("search");
            try {
                ArrayList<Book> bookList = bookDao.getBook(book);
                request.setAttribute("bookList", bookList);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/book/search.jsp");
            requestDispatcher.forward(request, response);
        }

        if ("borrowBook".equals(method)) {
            int bookId = Integer.parseInt(request.getParameter("id"));
            int userId = (int) request.getSession().getAttribute("user_id");
            Date date = new Date();
            logger.info("borrowBook with bookId:" + bookId + " userId:" + userId + " date:" + date);
            if (bookRecordDao.borrowBook(bookId, userId, date)) {
                request.getSession().setAttribute("user_borrow_message", "借书成功");
            } else {
                request.getSession().setAttribute("user_borrow_message", "借书失败");
            }
            response.sendRedirect("/record/list.jsp");
        }

        if ("backBook".equals(method)) {
            int id = Integer.parseInt(request.getParameter("id"));
            Date date = new Date();
            if (bookRecordDao.backBook(date, id)) {
                request.getSession().setAttribute("user_back_message", "还书成功");
            } else {
                request.getSession().setAttribute("user_back_message", "借书成功");
            }
            response.sendRedirect("/record/list.jsp");
        }

        if ("recordList".equals(method)) {
            // 获取当前登录的用户的用户ID
            if (request.getSession().getAttribute("user_id") != null) {
                int userId = (int) request.getSession().getAttribute("user_id");
                logger.info("recordList by userId:" + userId);
                try {
                    ArrayList<BookRecord> arrayList = bookRecordDao.getUserRecordList(userId);
                    request.setAttribute("user_bookRecord", arrayList);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            requestDispatcher = servletContext.getRequestDispatcher("/view/record_list.jsp");
            requestDispatcher.forward(request, response);
        }
    }
}
